﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using DocPlat.Library;

namespace DocPlat.UI
{
    public partial class BankList : UserControl
    {
        ServerObject mServerObject = null;
        bool mNewRecord = false;

        public BankList()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this);   
        }

        private void FillBankGridView()
        {
            DataTable dsource = mServerObject.GetBankList();
            BindingSource bindigSource = new BindingSource(dsource, null);
            bankGridView.DataSource = bindigSource;

            if (bankGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in bankGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                bankGridView.AutoResizeColumns();
            }

            if (dsource == null || dsource.Rows.Count == 0)
            { mNewRecord = true; }
        }


        private void cancelButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(bankGridView, 5);
            tableLayoutPanel2.Visible = false;
            mNewRecord = false;

            idTextBox.Text = string.Empty;
            contTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
        }

        private void BankList_Load(object sender, EventArgs e)
        {
            FillBankGridView();

            tableLayoutPanel1.SetColumnSpan(bankGridView, 5);
            tableLayoutPanel2.Visible = false;

            idTextBox.Text = string.Empty;
            contTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
        }

        private void editBankList()
        {
            tableLayoutPanel1.SetColumnSpan(bankGridView, 4);
            tableLayoutPanel2.Visible = true;

            if (bankGridView.SelectedRows.Count == 1)
            {
                idTextBox.Text = bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["id"].Value.ToString();
                descriptionTextBox.Text = bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["Description"].Value.ToString();
                contTextBox.Text = bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["Cont"].Value.ToString();

                if (bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["isDefault"].Value.ToString().Equals("True"))
                {
                    isDefaultCheckBox.Checked = true;
                }
                else
                { 
                    isDefaultCheckBox.Checked = false; 
                }
            }
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            editBankList();
        }

        private void bankGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            editBankList();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillBankGridView();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(bankGridView, 5);
            tableLayoutPanel2.Visible = false;

            bool resultOperation = false;
            if (mNewRecord)
            {
                resultOperation = mServerObject.AddBank(descriptionTextBox.Text.Trim(), contTextBox.Text.Trim(), isDefaultCheckBox.Checked);
            }
            else
            {
                int bankID = 0;
                if (bankGridView.SelectedRows.Count == 1)
                {
                    int.TryParse(bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out bankID);
                }

                if (bankID != 0)
                    resultOperation = mServerObject.UpdateBank(bankID, descriptionTextBox.Text.Trim(), contTextBox.Text.Trim(), isDefaultCheckBox.Checked);
            }

            if (!resultOperation)
            {
                MessageBox.Show("Failure savind data!");
            }

            FillBankGridView();

            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            contTextBox.Text = string.Empty;

            mNewRecord = false;
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(bankGridView, 4);
            tableLayoutPanel2.Visible = true;
            mNewRecord = true;

            idTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
            contTextBox.Text = string.Empty;
            isDefaultCheckBox.Checked = false;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure ???", "Attention!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int idBank = 0;
                int.TryParse(bankGridView.Rows[bankGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out idBank);

                if (idBank == 0 || !mServerObject.DeleteBank(idBank))
                {
                    MessageBox.Show("Error deleting record!");
                }

                FillBankGridView();

                idTextBox.Text = string.Empty;
                descriptionTextBox.Text = string.Empty;
                contTextBox.Text = string.Empty;
                isDefaultCheckBox.Checked = false;
            }
        }


    }
}
